gulp              = require 'gulp'
fs                = require 'fs'
gutil             = require 'gulp-util'
bower             = require 'bower'
concat            = require 'gulp-concat'
sass              = require 'gulp-sass'
minifyCss         = require 'gulp-minify-css'
minifyHtml        = require 'gulp-minify-html'
rename            = require 'gulp-rename'
sh                = require 'shelljs'
clean             = require 'gulp-clean'
processHtml       = require 'gulp-processhtml'
imagemin          = require 'gulp-imagemin'
pngquant          = require 'imagemin-pngquant'
stripDebug        = require 'gulp-strip-debug'
uglify            = require 'gulp-uglify'
templateCache     = require 'gulp-angular-templatecache'
useref            = require 'gulp-useref'
gulpif            = require 'gulp-if'
gulpSequence      = require 'gulp-sequence'
jshint            = require 'gulp-jshint'
map               = require 'map-stream'
historyApiFallback= require 'connect-history-api-fallback'
browserSync       = require('browser-sync').create()

paths =
  components: './app/components/'
  scss: ['./app/**/*.scss', '!./app/lib/']

gulp.task 'scss', (done) ->
  gulp.src paths.scss
    .pipe(sass())
    .on('error', sass.logError)
    # .pipe(minifyCss(keepSpecialComments: 0))
    .pipe(gulp.dest('./app/'))
    # .on 'end', done


gulp.task 'watch', ['scss'], (done) ->
  gulp.watch paths.scss, ['scss']

myReporter = map((file, cb) ->
  if !file.jshint.success
    console.log 'JSHINT fail in ' + file.path
    file.jshint.results.forEach (err) ->
      if err
        console.log err.error.line
        # console.log ' ' + file.path + ': line ' + err.line + ', col ' + err.character + ', code ' + err.code + ', ' + err.reason
      return
  cb null, file
  return
)

gulp.task 'lint', ->
  gulp.src('./app/components/**/*.js').pipe(jshint()).pipe myReporter


create_js = (name) ->
  js_tml = "angular.module('" + name + "', [])\n.controller('" + name + "Ctrl', ['$scope', 'httpService', function($scope, httpService) {\n\n}]);"
  js_tml

create_css = (name) ->
  css_tml = "@charset 'UTF-8';\n@import
            '../../config/scss/function.scss',
            '../../config/scss/config.scss';\n
            ." + name + " {\n\n}"
  css_tml

create_html = (name) ->
  html_tml = '<!-- ' + name + ' 页面内容 -->\n<section class="' + name + '" ng-controller="' + name + 'Ctrl">\n\n</section>'
  html_tml

gulp.task 'file', () ->
  if gulp.env._[0] is 'file'
    t = gulp.env.t
    file = paths.components + t + '/'
    console.log('file', file)
    fs.mkdir file, (err) ->
      fs.writeFile file + t + '.js', create_js(t), (err, fd) ->
        console.log '创建js文件-成功'
        return
      fs.writeFile file + t + '.scss', create_css(t), (err, fd) ->
        console.log '创建scss文件-成功'
        return
      fs.writeFile file + t + '.html', create_html(t), (err, fd) ->
        console.log '创建html文件-成功'
        return
      fs.mkdir file + 'imgs', (err) ->





gulp.task 'clean', (done) ->
  gulp.src './www'
    .pipe clean()

gulp.task 'useref', (done) ->
  gulp.src './app/index.html'
    .pipe useref()
    .pipe gulpif('*.js', uglify())
    .pipe gulpif('*.css', minifyCss())
    .pipe gulp.dest('./www')
    # .on 'end', done

gulp.task 'js', (done) ->
  gulp.src ['./app/**/*.js', '!./app/lib/**/*.js', '!./app/config/**/*.js']
    # .pipe uglify()
    .pipe gulp.dest('./www/')
    # .on 'end', done

gulp.task 'html', (done) ->
  gulp.src ['./app/**/*.html', '!./app/lib/**/*.html', '!./app/components/mine/orders/orderdetail/orderdetail.html']
    # .pipe minifyHtml()
    .pipe gulp.dest('./www/')
    # .on 'end', done

gulp.task 'img', (done) ->
  gulp.src(['./app/components/**/*.png', './app/components/**/*.jpg'])
    .pipe(imagemin())
    .pipe(gulp.dest './www/components/')
    # .on 'end', done
  gulp.src(['./app/favicon.png', './app/apple-touch-icon-ipad.png', './app/apple-touch-icon-iphone.png', './app/apple-touch-icon-iphone4.png'])
    .pipe gulp.dest('./www/')


gulp.task 'copy', (done) ->
  gulp.src('./app/lib/jquery/dist/jquery.min.js').pipe gulp.dest './www/lib/jquery/dist/'
  gulp.src('./app/lib/bootstrap/dist/js/bootstrap.min.js').pipe gulp.dest './www/lib/bootstrap/dist/js/'
  gulp.src('./app/lib/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js').pipe gulp.dest './www/lib/html5-boilerplate/dist/js/vendor/'
  gulp.src('./app/lib/jquery/dist/jquery.min.js').pipe gulp.dest './www/lib/jquery/dist/'
  gulp.src('./app/lib/angularjs-datepicker/dist/angular-datepicker.min.js').pipe gulp.dest './www/lib/angularjs-datepicker/dist/'
  gulp.src('./app/lib/angularjs-datepicker/dist/angular-datepicker.min.css').pipe gulp.dest './www/lib/angularjs-datepicker/dist/'
  gulp.src('./app/lib/bootstrap/fonts/*').pipe gulp.dest './www/fonts'
  gulp.src('./app/fonts/*').pipe gulp.dest('./www/css/fonts')
  gulp.src('./app/components/mine/orders/orderdetail/orderdetail.html').pipe gulp.dest('./www/components/mine/orders/orderdetail/')
  gulp.src('./app/lib/flatpickr/dist/l10n/zh.js').pipe gulp.dest('./www/lib/flatpickr/dist/l10n/')
  gulp.src('./app/lib/flatpickr/dist/flatpickr.min.css').pipe gulp.dest('./www/lib/flatpickr/dist/')
  gulp.src('./app/lib/flatpickr/dist/flatpickr.min.js').pipe gulp.dest('./www/lib/flatpickr/dist/')
  gulp.src('./app/lib/zoom.js/css/zoom.css').pipe gulp.dest('./www/lib/zoom.js/css/')
  gulp.src('./app/lib/zoom.js/dist/zoom.js').pipe gulp.dest('./www/lib/zoom.js/dist/')
  gulp.src('./app/lib/jQuery.print/jQuery.print.js').pipe gulp.dest('./www/lib/jQuery.print/')
  gulp.src('./app/pay.htm').pipe gulp.dest('./www/')
  gulp.src('./app/lib/highcharts/highcharts.js').pipe gulp.dest('./www/lib/highcharts/')
  gulp.src('./app/lib/highcharts/modules/exporting.js').pipe gulp.dest('./www/lib/highcharts/modules/')
  gulp.src('./app/lib/highcharts/themes/grid-light.js').pipe gulp.dest('./www/lib/highcharts/themes/')
  gulp.src('./app/lib/angular-touch/angular-touch.js').pipe gulp.dest('./www/lib/angular-touch/')
  gulp.src('./app/lib/echarts/echarts-all.js').pipe gulp.dest('./www/lib/echarts/')

  
  gulp.src ['./app/**/*.css', '!./app/lib/**/*.css', '!./app/app.css']
    .pipe gulp.dest('./www')

gulp.task 'serve', (done) =>
  browserSync.init({
    server: {
      baseDir: 'app',
      middleware: [ historyApiFallback() ]
    }
  })
  gulp.watch("app/**/**.scss", ['scss']).on('change', browserSync.reload)
  gulp.watch('app/**/**.js').on('change', browserSync.reload)
  gulp.watch('app/**/**.html').on('change', browserSync.reload)

gulp.task 'build', gulpSequence('clean', 'scss', 'img', 'js', 'html', 'copy', 'useref')





